<style> 
.cart_list_default .cart_order_btn .checkout{
	display: flex;
}
</style>
{% if cart.item_count != 0 %}
<div class="cart_list_default container_wrapper" id="cart_list">
	<h1 class="title">{{ lang.cart.list.your_cart }}</h1>
	<form class="cart_form" action="/cart" method="post" novalidate="">
		<div class="cart_product_list">
			<div class="cart_list_title">
				<div class="cart_list_title_items">{{ lang.cart.list.product }}</div>
				<div class="cart_list_title_items"></div>
				<div class="cart_list_title_items">{{ lang.cart.list.quantity }}</div>
				<div class="cart_list_title_items">{{ lang.cart.list.total }}</div>
				<div class="cart_list_title_items"></div>
			</div>
			<div class="cart_product_list——content">
				{% for list in cart.items %}
				<div class="cart_list_content">
				  	
					<div class="cart_list_content_items">
						<div class="product_info">
							<div class="product_info_pic"><a href="{{ list.url }}?data_from={{data_from}}"><img data-src="{{ list.image }}" src="{{ 'empty.png' | public_asset_abs_url }}"></a></div>
							
						</div>
					</div>
					<div class="cart_list_content_items" style="overflow: hidden;">
						<div class="product_info_info" >
							<a class="product_info_name" href="{{ list.url }}?data_from={{data_from}}">{{ list.product.title }}</a>
							{% unless list.product.has_only_default_variant %}
							<div class="product_info_sku">
								{% for option in list.options_with_values %}
								{% if forloop.last == true %}
								{{ option.name }}: {{ option.value }}
								{% else %}
								{{ option.name }}: {{ option.value }}, 
								{% endif %}	
								{% endfor %}
								
							</div>
							{% endunless %}
						</div>
					</div>
					<div class="cart_list_content_items">
						<div class="cart_qty_content">

							{% if list.product.inventory_tracking %}
								{% if list.product.inventory_policy != 2 and list.variant.inventory_quantity == 0 %}

								<div class="stock_quantity">{{ list.quantity }}<br /><br />{{ lang.checkout.out_of_stock }}</div>
								<div class="wap_stock_quantity">{{ lang.checkout.out_of_stock }}:{{ list.quantity }}</div>

								{% else %}
								<div class="cart_qty" inventory_policy="{{ list.product.inventory_policy }}" inventory_tracking="{{ list.product.inventory_tracking }}" inventory_quantity="{{ list.variant.inventory_quantity }}">
									<a class="cart_qty_minus" href="javascript:void(0);">{% include icon_minus ,{width:'16',height:'16'} %}</a>
									<input class="cart_qty_txt" type="text" name="cart_qty" value="{{ list.quantity }}">
									<a class="cart_qty_plus" href="javascript:void(0);">{% include icon_plus ,{width:'16',height:'16'} %}</a>
								</div>
								{% endif %}

							{% else %}
							<div class="cart_qty" inventory_policy="{{ list.product.inventory_policy }}" inventory_tracking="{{ list.product.inventory_tracking }}" inventory_quantity="{{ list.variant.inventory_quantity }}">
								<a class="cart_qty_minus" href="javascript:void(0);">{% include icon_minus ,{width:'16',height:'16'} %}</a>
								<input class="cart_qty_txt" type="text" name="cart_qty" value="{{ list.quantity }}">
								<a class="cart_qty_plus" href="javascript:void(0);">{% include icon_plus ,{width:'16',height:'16'} %}</a>
							</div>
							{% endif %}
							{% if list.product.inventory_tracking %}
							{% if list.quantity > list.variant.inventory_quantity %}
							<div class="tip">{{ lang.general.stock }}: {{ list.variant.inventory_quantity }}</div>
							{% endif %}
							{% endif %}
						</div>
					</div>
					<div class="cart_list_content_items product_total">{{ list.final_line_price | money }}</div>
					<div class="cart_list_content_items">
						<a class="cart_list_delete" href="javascript:void(0);">{% include icon_delete ,{width:'20',height:'20'} %}</a>
					</div>
				</div>
				{% endfor %}
			</div>
		</div>
		<div id="overstep_prompt"></div>
		<div class="cart_order_summary">
			<div class="cart_order_summary_wrapper">
				<div class="cart_order_price">
					<ul>
						{% unless cart.total_discount == 0 %}
						<li>
							<span>{{ lang.cart.list.discount }}</span>
							<span id="cart_discount">{{ cart.total_discount | money }}</span>
						</li>
						{% endunless %}
						
						<li id="subtotal">
							<span>{{ lang.cart.list.subtotal }}</span>
							<span id="cart_subtotal">{{ cart.items_subtotal_price | money }}</span>
						</li>
                        {% if cart.promotion_price %}
                        <li id="promotion_price_box">
                            <span>{{ lang.checkout.promotion_discount }}</span>
                            <span id="promotion_price">{{ cart.promotion_price | money }}</span>
                        </li>
                        {% endif %}
                        <li><span>{{ lang.cart.list.taxes_and_shipping }}</span><span>{{ lang.cart.list.calculated_at_checkout }}</span></li>
						<li>
							<span>{{ lang.cart.list.total }}</span>
							<span id="cart_total_price">{{ cart.total_price | money }}</span>
						</li>
					</ul>
					<div class="cart_order_remark"></div>
				</div>
				<div class="cart_order_btn">
					{% if cart.has_out_of_stock %}
					<a class="checkout main_btn checkout_out_of_stock" href="javascript:void(0);">{{ lang.cart.list.checkout }}</a>
					{% else %}
					<a class="checkout main_btn" id="checkout" href="javascript:void(0);">{{ lang.cart.list.checkout }}</a>
					
					{% endif %}
				</div>
				
                {% if payment_form_html %}
                <div class="shopcart-or" {% if cart.has_out_of_stock %}style="display:none;"{% endif %}><span>{{ lang.cart.list.or }}</span></div>   
                <div class="shopcart-payment" {% if cart.has_out_of_stock %}style="display:none;"{% endif %}>{{ payment_form_html }}</div>
                {% endif %}
                
				<div class="cart_order_link"><a class="links" href="/">< {{ lang.cart.list.continue_shopping }}</a></div>
				
			</div>
		</div>
	</form>
</div>
<script type="text/javascript">
	$(function(){
		const cart = {{cart|json}};
		var minus = $('.cart_qty_minus');
		var plus = $('.cart_qty_plus');
		var qtyText = $('.cart_qty_txt');
		minus.click(function(){
			var _this = $(this);
			var _index = _this.parents('.cart_list_content').index();
			var line = parseInt(_index) + 1;
			var qtyVal = _this.siblings('.cart_qty_txt').attr('value');
			var inventory_policy = _this.parent().attr('inventory_policy');
			var inventory_tracking = _this.parent().attr('inventory_tracking');
			var inventory_quantity = _this.parent().attr('inventory_quantity');
			var newQtyVal;

			if(qtyVal >= 2 && qtyVal <= 999999){
				newQtyVal = parseInt(qtyVal) - 1;
				_this.siblings('.cart_qty_txt').attr('value',newQtyVal);

			}else if(parseInt(qtyVal) > 999999){
				newQtyVal = 999999;
				_this.siblings('.cart_qty_txt').attr('value','999999');
			}else{
				return false;
			}

			if(inventory_tracking == '1'){
				if(parseInt(newQtyVal) > parseInt(inventory_quantity)){
					if(_this.parent().siblings('.tip').length == 0){
						_this.parent().after('<div class="tip">{{ lang.general.stock }}: '+ inventory_quantity +'</div>');
					}
				}else{
					_this.parent().siblings('.tip').remove();
				}
			}
			
			if(qtyVal > 1){
				moi.ajax({
					url:'/cart/change',
					type: 'POST',
					contentType: "application/json",
					data:JSON.stringify({'line':line,'quantity':newQtyVal}),
	                dataType: 'json',
					success: function(res){

                        if(typeof(res.data.items[(line - 1)]) == 'undefined')
                        {
                            window.location.reload();
                        }
						var price1 = res.data.items[(line - 1)].final_line_price;
						var price2 = res.data.total_discount;
						var price3 = res.data.items_subtotal_price;
						var price4 = res.data.total_price;
						var promotion = res.data.promotion_price;
						_this.parents('.cart_list_content').find('.product_total').html(formatMoney(price1, oemcart_currency));
						$('#cart_discount').html(formatMoney(price2, oemcart_currency));
						$('#cart_subtotal').html(formatMoney(price3, oemcart_currency));
						$('#cart_total_price').html(formatMoney(price4, oemcart_currency));
						addCartNum();
						miniCart();
						if(promotion == 0 && $('#promotion_price_box').length != 0){
							$('#promotion_price_box').remove();
						}else if(promotion != 0){
							if($('#promotion_price_box').length == 0){
								$('#subtotal').after('<li id="promotion_price_box"><span>{{ lang.checkout.promotion_discount }}</span><span id="promotion_price">'+ formatMoney(promotion, oemcart_currency) +'</span></li>');
							}else{
								$('#promotion_price').html(formatMoney(promotion, oemcart_currency));
							}
							
						}
						if(res.data.has_out_of_stock){
							$('.checkout').removeAttr('id');
							$('.checkout').addClass('checkout_out_of_stock');
							$('.shopcart-payment').hide();
							$('.shopcart-or').hide();
						}else{
							$('.checkout').attr('id','checkout');
							$('.checkout').removeClass('checkout_out_of_stock');
							$('.shopcart-payment').show();
							$('.shopcart-or').show();
						}

					},
					beforeSend: function(){
			           createAjaxLoading();
			        },
			        complete: function(){
			            removeAjaxLoading();
			        }
				})
			}
		})
		plus.click(function(){
			
			var _this = $(this);
			var _index = _this.parents('.cart_list_content').index();
			var line = parseInt(_index) + 1;
			var qtyVal = _this.siblings('.cart_qty_txt').attr('value');
			var inventory_policy = _this.parent().attr('inventory_policy');
			var inventory_tracking = _this.parent().attr('inventory_tracking');
			var inventory_quantity = _this.parent().attr('inventory_quantity');
			var newQtyVal;
			if(qtyVal >= 0 && qtyVal <= 999999){
				newQtyVal = parseInt(qtyVal) + 1;
				_this.siblings('.cart_qty_txt').attr('value',newQtyVal);
			}else if(parseInt(qtyVal) > 999999){
				newQtyVal = 999999;
			}
			if(inventory_tracking == '1'){
				if(parseInt(newQtyVal) > parseInt(inventory_quantity)){
					if(_this.parent().siblings('.tip').length == 0){
						_this.parent().after('<div class="tip">{{ lang.general.stock }}: '+ inventory_quantity +'</div>');
					}
				}else{
					_this.parent().siblings('.tip').remove();
				}
			}

			moi.ajax({
				url:'/cart/change',
				type: 'POST',
				contentType: "application/json",
				data:JSON.stringify({'line':line,'quantity':newQtyVal}),
                dataType: 'json',
				success: function(res){
                    if(typeof(res.data.items[(line - 1)]) == 'undefined')
                    {
                        window.location.reload();
                    }
					var price1 = res.data.items[(line - 1)].final_line_price;
					var price2 = res.data.total_discount;
					var price3 = res.data.items_subtotal_price;
					var price4 = res.data.total_price;
					var promotion = res.data.promotion_price;


					_this.parents('.cart_list_content').find('.product_total').html(formatMoney(price1, oemcart_currency));
					$('#cart_discount').html(formatMoney(price2, oemcart_currency));
					$('#cart_subtotal').html(formatMoney(price3, oemcart_currency));
					$('#cart_total_price').html(formatMoney(price4, oemcart_currency));
					addCartNum();
					miniCart();
					if(promotion == 0 && $('#promotion_price_box').length != 0){
						$('#promotion_price_box').remove();
					}else if(promotion != 0){
						if($('#promotion_price_box').length == 0){
							$('#subtotal').after('<li id="promotion_price_box"><span>{{ lang.checkout.promotion_discount }}</span><span id="promotion_price">'+ formatMoney(promotion, oemcart_currency) +'</span></li>');
						}else{
							$('#promotion_price').html(formatMoney(promotion, oemcart_currency));
						}
						
					}
					if(res.data.has_out_of_stock){
						$('.checkout').removeAttr('id');
						$('.checkout').addClass('checkout_out_of_stock');
						$('.shopcart-payment').hide();
						$('.shopcart-or').hide();
					}else{
						$('.checkout').attr('id','checkout');
						$('.checkout').removeClass('checkout_out_of_stock');
						$('.shopcart-payment').show();
						$('.shopcart-or').show();
					}

					
				},
				beforeSend: function(){
		           createAjaxLoading();
		        },
		        complete: function(){
		            removeAjaxLoading();
		        }
			})
		})
		qtyText.keyup(function(){
			$(this)[0].value = $(this)[0].value.replace(/^(0+)|[^\d]+/g,'');
			if($(this)[0].value > 999999){
				$(this)[0].value = 999999;
			}
		})
		qtyText.blur(function(){
			if($(this).attr('value') == ""){
				$(this).attr('value','1');
			}
		})
		qtyText.change(function(){
			
			var _this = $(this);
			var _index = _this.parents('.cart_list_content').index();
			var line = parseInt(_index) + 1;
			var qtyVal = _this.attr('value');
			if(qtyVal == ''){
				qtyVal = 1;
			}
			var inventory_policy = _this.parent().attr('inventory_policy');
			var inventory_tracking = _this.parent().attr('inventory_tracking');
			var inventory_quantity = _this.parent().attr('inventory_quantity');

			var newQtyVal;
			if(parseInt(qtyVal) >= 0 && qtyVal <= 999999){
				newQtyVal = parseInt(qtyVal);
				_this.siblings('.cart_qty_txt').attr('value',newQtyVal);
			}else if(parseInt(qtyVal) > 999999){
				newQtyVal = 999999;
			}
			if(inventory_tracking == '1'){
				if(parseInt(newQtyVal) > parseInt(inventory_quantity)){
					qtyVal = inventory_quantity;
					if(_this.parent().siblings('.tip').length == 0){
						_this.parent().after('<div class="tip">{{ lang.general.stock }}: '+ inventory_quantity +'</div>');
					}
				}else{
					_this.parent().siblings('.tip').remove();
				}
			}

			moi.ajax({
				url:'/cart/change',
				type: 'POST',
				contentType: "application/json",
				data:JSON.stringify({'line':line,'quantity':newQtyVal}),
                dataType: 'json',
				success: function(res){
                    if(typeof(res.data.items[(line - 1)]) == 'undefined')
                    {
                        window.location.reload();
                    }
					var price1 = res.data.items[(line - 1)].final_line_price;
					var price2 = res.data.total_discount;
					var price3 = res.data.items_subtotal_price;
					var price4 = res.data.total_price;
					var promotion = res.data.promotion_price;
					_this.parents('.cart_list_content').find('.product_total').html(formatMoney(price1, oemcart_currency));
					$('#cart_discount').html(formatMoney(price2, oemcart_currency));
					$('#cart_subtotal').html(formatMoney(price3, oemcart_currency));
					$('#cart_total_price').html(formatMoney(price4, oemcart_currency));
					addCartNum();
					miniCart();
					if(promotion == 0 && $('#promotion_price_box').length != 0){
						$('#promotion_price_box').remove();
					}else if(promotion != 0){
						if($('#promotion_price_box').length == 0){
							$('#subtotal').after('<li id="promotion_price_box"><span>{{ lang.checkout.promotion_discount }}</span><span id="promotion_price">'+ formatMoney(promotion, oemcart_currency) +'</span></li>');
						}else{
							$('#promotion_price').html(formatMoney(promotion, oemcart_currency));
						}
					}
					if(res.data.has_out_of_stock){
						$('.checkout').removeAttr('id');
						$('.checkout').addClass('checkout_out_of_stock');
						$('.shopcart-payment').hide();
						$('.shopcart-or').hide();
					}else{
						$('.checkout').attr('id','checkout');
						$('.checkout').removeClass('checkout_out_of_stock');
						$('.shopcart-payment').show();
						$('.shopcart-or').show();
					}
				},
				beforeSend: function(){
		           createAjaxLoading();
		        },
		        complete: function(){
		            removeAjaxLoading();
		        }
			})
		})
		$('.cart_list_delete').click(function(){
			var _this = $(this);
			var _index = _this.parents('.cart_list_content').index();
			var line = parseInt(_index) + 1;
			moi.ajax({
				url:'/cart/change',
				type: 'POST',
				contentType: "application/json",
				data:JSON.stringify({'line':line,'quantity':0}),
        dataType: 'json',
				success: function(res){
					const item = cart.items[_index];
					const product = item.product;
					const variant = item.variant || product.variant;
					if (window.callback_add_to_cart) {
						const ec_data = {
							'ec_data_id': product['id'],
							'ec_data_from': '{{ data_from }}',
							'ec_data_quantity': item.quantity,
							'ec_data_name': product['title'],
							'ec_data_price': variant['price'],
							'ec_data_discount_price': variant['price'],
							'ec_data_spu': product['spu'],
							'ec_data_image': product['src'],
							'ec_data_category': product['product_type'],
							'ec_data_uniq_id': product['id'],
							'ec_data_sku': variant['sku'],
							'ec_data_sku_code': variant['sku_code'],
							'ec_data_sku_name': variant['sku_value'],
							'ec_currency_code': '{{ base_currency.standard_code }}',
							'ec_data_action_type': 'remove_from_cart'
						};
						callback_remove_from_cart(ec_data)
					}

					if(res.data.items.length == 0){
						location.reload();
						
					}else{
						_this.parents('.cart_list_content').remove();
						var price2 = res.data.total_discount;
						var price3 = res.data.items_subtotal_price;
						var price4 = res.data.total_price;
						var promotion = res.data.promotion_price;
						$('#cart_discount').html(formatMoney(price2, oemcart_currency));
						$('#cart_subtotal').html(formatMoney(price3, oemcart_currency));
						$('#cart_total_price').html(formatMoney(price4, oemcart_currency));
						addCartNum();
						miniCart();
						if(promotion == 0 && $('#promotion_price_box').length != 0){
							$('#promotion_price_box').remove();
						}else if(promotion != 0){
							if($('#promotion_price_box').length == 0){
								$('#subtotal').after('<li id="promotion_price_box"><span>{{ lang.checkout.promotion_discount }}</span><span id="promotion_price">'+ formatMoney(promotion, oemcart_currency) +'</span></li>');
							}else{
								$('#promotion_price').html(formatMoney(promotion, oemcart_currency));
							}
							
						}
					}
					
					if(res.data.has_out_of_stock){
						$('.checkout').removeAttr('id');
						$('.checkout').addClass('checkout_out_of_stock');
						$('.shopcart-payment').hide();
						$('.shopcart-or').hide();
					}else{
						$('.checkout').attr('id','checkout');
						$('.checkout').removeClass('checkout_out_of_stock');
						$('.shopcart-payment').show();
						$('.shopcart-or').show();
					}
				},
				beforeSend: function(){
					createAjaxLoading()
		        },
		        complete: function(){
		            removeAjaxLoading()
		        }
			})
		})
		$('.cart_form').submit(function(){
            var is_submit = true;
          	

            return is_submit;
         });
		$(document).on('click','#checkout',function(){
			$('.cart_form').submit();
		})

		
	})



	
</script>
{% else %}
<div class="cart_empty_default" id="cart_empty">
	<div class="cart_empty_wrapper">
		
		<div class="cart_empty_content">
			<h3 class="title">{{ lang.cart.list.shopping_cart }}</h3>
			<div class="cart_empty_svg">{% include icon_cart ,{width:'100',height:'100'} %}</div>
			<p>{{ lang.cart.list.your_shopping_cart_is_empty }}</p>
		</div>
		<a class="continue_shopping_btn" href="/">{{ lang.cart.list.continue_shopping }}</a>
		
	</div>
</div>
{% endif %}